new Vue({
  el: '#app',
  data() {
    return {
      rules: {
        // Warehouse_Name: [{ required: true, message: '请输入仓库名称', trigger: 'blur' }],
        // Warehouse_Type: [{ required: true, message: '请选择仓库类型', trigger: 'blur' }],
        // Applicable_Type: [{ required: true, message: '请选择仓库性质', trigger: 'blur' }],
        // address: [{ required: true, message: '请输入详细地址', trigger: 'blur' }]
      },
      city: [],
      address: '',
      value: [],
      form: {
        Warehouse_SerialNumber: '',
        Warehouse_Name: '',
        Warehouse_Address: '',
        Warehouse_Type: '',
        Applicable_Type: '0',
        Parent_Warehouse_SerialNumber: '',
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
      },
      warehouse: [],
      loading: false
    }
  },
  mounted() {
    this.get_original_value(sessionStorage.getItem("Warehouse_SerialNumber"))
    this.getData()
  },
  methods: {
    get_original_value(Primary_Key) {
      console.log(Primary_Key);
      let _this = this
      $.ajax({
        url: DomainName + '/getSubtableInquiryOriginal?token=' + $.cookie("token"),
        type: 'POST',
        data: {
          JSONObject: JSON.stringify({
            warehouse_info: { warehouse_inventory: {} }
          }),
          Primary_Key: Primary_Key
        },
        success: function (res) {
          console.log(res);
          _this.form = res.warehouse_info[0];
          _this.value = res.warehouse_info[0].Warehouse_Address.split('/').slice(0, 3)
          _this.address = res.warehouse_info[0].Warehouse_Address.split('/')[3]
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    getData() {
      const _this = this
      $.ajax({
        url: "../../../js/city.json",
        data: { action: "jQueryAjax" },
        type: "GET",
        success: function (data) {
          _this.city = data
          console.log(data);
        },
        dataType: "json"
      })
      $.ajax({
        type: 'GET',
        url: `${DomainName}/getWarehouseTypeAll?token=${$.cookie('token')}`,
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        success: function (Data) {
          _this.warehouse = Data
        },
        dataType: "json"
      });
    },
    handleType(e) {
      this.form.Warehouse_Name = ""
      this.form.Warehouse_Type = ""
      this.form.Warehouse_Address = ""
      this.form.Parent_Warehouse_SerialNumber = ""
      this.value = []
      this.address = ""
    },
    handleWarehouse(e) {
      this.warehouse.forEach(item => {
        if (item.Warehouse_SerialNumber == e) {
          this.form.Warehouse_Name = item.Warehouse_Name
          this.form.Warehouse_Type = item.Warehouse_Type
          this.form.Warehouse_Address = item.Warehouse_Address
          this.value = item.Warehouse_Address.split('/').slice(0, 3)
          this.address = item.Warehouse_Address.split('/')[3]
        }
      })
    },
    // address(value) {
    //   console.log(value);
    // },
    handleChange(e) {
      this.form.Warehouse_Address = `${this.value[0]}/${this.value[1]}/${this.value[2]}`
      console.log(this.form.Warehouse_Address);
    },
    handleChange2() { this.form.Warehouse_Address = `${this.value[0]}/${this.value[1]}/${this.value[2]}/${e}` },
    submitForm() {
      const _this = this;
      _this.loading = true
      const { add, modify } = _this.deal()
      let data = {}
      if (add == null) {
        data = { Data: JSON.stringify({ Modify: { warehouse_info: [modify] } }) }
      } else {
        data = {
          Data: JSON.stringify(
            {
              Modify: { warehouse_info: [modify] },
              Add: { warehouse_info: [add] }
            }
          ),
        }
      }
      console.log(this.value);
      $.ajax({
        url: DomainName + '/getEditAction?token=' + $.cookie("token"),
        type: 'POST',
        data,
        success: function (res) {
          console.log('提交结果', res)
          _this.$message.success("提交成功")
          window.history.go(-1);
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    deal() {
      const add = deepClone(this.form)
      const modify = deepClone(this.form)
      let add_warehouse_inventory = [], modify_warehouse_inventory = []
      for (const i in modify.Children.warehouse_inventory) {
        const obj = modify.Children.warehouse_inventory[i];
        if (!Object.keys(obj).some(item => item.includes('_Original'))) {
          add_warehouse_inventory.push(obj)
        } else {
          modify_warehouse_inventory.push(obj)
        }
      }
      add.Children.warehouse_inventory = add_warehouse_inventory

      modify.Children.warehouse_inventory = modify_warehouse_inventory

      if (add_warehouse_inventory.length == 0) {
        return { add: null, modify };
      }
      return { add, modify };
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  }
})